<?php
	if(!defined('BASEPATH')) exit('No direct script access allowed');
	/**
	*  全局通用函数
	*/
	

	/*
	*  消息
	*  @message string 指定显示的主要消息
	*  @href array 附加的跳转链接 键值分别代表文本和地址
	*  @type string 特定的字符串，决定消息的图标 success:普通消息 warning:警示 
	*  @timeout 自动跳转时间
	*/
	function showmessage($message = 'no message!',$href=array(),$type='success',$timeout=3){
		$CI = & get_instance();
		//$data = func_get_args()
		switch($type){
			case 'warning' :$status='warning';break;
			case 'error' :$status='remove';break;
			case 'info' :$status='info';break;
			case 'success' :$status='ok';break;
			default :$status='info';break;
		}
		$data = array(
			'message' => $message,
			'href' => $href,
			'type' => $type,
			'status' => $status,
			'timeout' => $timeout
		);
		
		
		//刷新地址
		if($backrefresh = $CI->session->userdata('backrefresh')){
			$data['backrefresh'] = site_url($backrefresh);
			$CI->session->unset_userdata('backrefresh');
			
		}
		else if($crefresh = $CI->session->userdata('crefresh')){
			$data['crefresh'] = site_url($crefresh);
			$CI->session->unset_userdata('crefresh');
		}
		
		
		//跳转连接
		if($location = $CI->session->userdata('location')){
			$data['location'] = site_url($location);
			$CI->session->unset_userdata('location');
		}
		

		ob_start();
			$CI->load->view('templates/header');
			$CI->load->view('templates/message',$data);
			$CI->load->view('templates/footer');
			
			$buffer = ob_get_contents();
		ob_end_clean();
	
		echo  $buffer;
		exit;
		
	}
	
	/*
	* 生成并设置验证码
	* @pre_captcha  存储的验证码相关session键名
	*/
	function captcha($pre_captcha = 'captcha',$width = false, $height = false){
		$CI = & get_instance();
		$CI->load->helper('captcha');
		
		$captcha_path = './data/captcha/';
		
		if(!file_exists($captcha_path)){
			mkdir($captcha_path,0555,true);
		}
		$data = array(
			'img_path' => $captcha_path,
			'img_url' => base_url().$captcha_path,
			'img_width' => '150',
			'img_height' => '30',
			'expiration' => 7200

		);
		if($width){
			$data['img_width'] = $width;
		}
		if($height){
			$data['img_height'] = $height;
		}
			
		$cap = create_captcha($data);
		$session = array(
			'word' => $cap['word'],
			'captcha_time' => $cap['time']
		);
		set_session($pre_captcha,$session);
		
		return  $cap;
		
	}
	
	
	//用于快速设定session
	function set_session($key,$value=''){
		$CI = & get_instance();
		$CI->load->library('session');
		
		$CI->session->set_userdata(array($key=>$value));
	}
	
	//前端错误面板
	function error($error){
?>
<div class="tips tips-large tips-warning">
          
          <div class="tips-content">
            <p class="auxiliary-text">
             <span class="x-icon x-icon-small x-icon-error">×</span>&nbsp;&nbsp;提示:发生了以下问题,更正后继续:)
            </p>
            <p>
			<?php
				if(is_array($error)){
					foreach($error as $e){
						echo $e."<br/>";
					}
				}else{
					echo $error;
				}
			?>
            </p>
          </div>
</div>
<?php
	}
	
	//设置闪存
	function set_flash($key,$value=''){
		$CI = & get_instance();
		$CI->load->library('session');
		
		$CI->session->set_flashdata($key,$value);
	}

?>